package com.kingstar.ceres.config;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 
import javax.sql.DataSource;
 
@Configuration
@MapperScan(basePackages = "com.kingstar.ceres.mapper.secondary",
    sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {
  @Value("${spring.datasource.secondary.driver-class-name}")
  private String driverClassName;
  @Value("${spring.datasource.secondary.url}")
  private String url;
  @Value("${spring.datasource.secondary.username}")
  private String username;
  @Value("${spring.datasource.secondary.password}")
  private String password;
  @Value("${spring.datasource.secondary.max}")
  private Integer max;
  @Value("${spring.datasource.secondary.min}")
  private Integer min;
 
  @Bean(name = "secondaryDataSource")
  public DataSource secondaryDataSource() {
//    DruidDataSource dataSource=new DruidDataSource();
//    dataSource.setUrl(url);
//    dataSource.setUsername(username);
//    dataSource.setPassword(password);
//    dataSource.setDriverClassName(driverClassName);

    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setJdbcUrl(url);
    dataSource.setDriverClassName(driverClassName);
    dataSource.setMaximumPoolSize(max);
    dataSource.setMinimumIdle(min);
    dataSource.setPoolName("secondaryDataSourcePool");
    return dataSource;
  }
 
  /**
   * local数据源
   */
  @Bean(name = "secondarySqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

//    MybatisConfiguration configuration = new MybatisConfiguration();
////    configuration.addInterceptor(new PerformanceInterceptor());
//    configuration.setLogImpl(StdOutImpl.class);
//    bean.setConfiguration(configuration);

    bean.setDataSource(dataSource);

    // 设置Mybatis全局配置路径
//    bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
    bean.setMapperLocations(
        new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/secondary/*.xml"));
    return bean.getObject();
  }
 
  @Bean(name = "secondaryTransactionManager")
  public DataSourceTransactionManager transactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
 
  @Bean(name = "secondarySqlSessionTemplate")
  public SqlSessionTemplate testSqlSessionTemplate(
      @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}